#include <string>
#include <stack>
#include <algorithm>
#include <iostream>

using namespace std;

class Solution 
{
public:
    string removeDuplicates(string s) 
    {
        stack<char> st;
        for(int i = 0 ; i < s.length(); i++)
        {
            if(!st.empty())
            {
                if(s[i] == st.top())
                {
                    st.pop();
                }
                else
                {
                    st.push(s[i]);
                }
            }
            else
            {
                st.push(s[i]);
            }
        }
        string result;
        /*
        循环条件 i < st.size() 中，st.size() 在每次迭代中都会变化，因为你调用了 st.pop()
        */
        int size = st.size();
        for(int i = 0; i < size; i++)
        {
            result += st.top();
            st.pop();
        }
        reverse(result.begin(), result.end());
        return result;
    }
};

int main()
{
    Solution solution;

    // 测试案例1: 基本相邻重复删除
    string test1 = "abbaca";
    cout << "测试案例1: " << test1 << endl;
    cout << "结果: " << solution.removeDuplicates(test1) << endl;
    cout << "预期: ca" << endl << endl;

    // 测试案例2: 无相邻重复
    string test2 = "abc";
    cout << "测试案例2: " << test2 << endl;
    cout << "结果: " << solution.removeDuplicates(test2) << endl;
    cout << "预期: abc" << endl << endl;

    // 测试案例3: 全是相同字符
    string test3 = "aaaa";
    cout << "测试案例3: " << test3 << endl;
    cout << "结果: " << solution.removeDuplicates(test3) << endl;
    cout << "预期: " << endl << endl;

    // 测试案例4: 连续不同字符
    string test4 = "aabccba";
    cout << "测试案例4: " << test4 << endl;
    cout << "结果: " << solution.removeDuplicates(test4) << endl;
    cout << "预期: abcba" << endl << endl;

    // 测试案例5: 空字符串
    string test5 = "";
    cout << "测试案例5: " << test5 << endl;
    cout << "结果: " << solution.removeDuplicates(test5) << endl;
    cout << "预期: " << endl << endl;

    // 测试案例6: 单个字符
    string test6 = "a";
    cout << "测试案例6: " << test6 << endl;
    cout << "结果: " << solution.removeDuplicates(test6) << endl;
    cout << "预期: a" << endl << endl;

    // 测试案例7: 复杂情况
    string test7 = "abbacaa";
    cout << "测试案例7: " << test7 << endl;
    cout << "结果: " << solution.removeDuplicates(test7) << endl;
    cout << "预期: c" << endl << endl;

    return 0;
}